<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <div id="app"></div>
<script>
  const data = {
    msg: 'hell',
    name: 'Zhang San'
  }
  const vm = {}
  /*Object.defineProperty(vm, 'msg', {
    enumerable: true,
    configurable: true,
    get() {
      console.log(`get: ${data.msg}`)
      return data.msg
    },
    set(msg) {
      console.log(`set: ${msg}`)
      if (msg === data.msg)  {
        return
      }
      data.msg = msg
      document.getElementById('app').textContent = data.msg;
    }
  })*/

  function proxyData(data) {
    Object.keys(data).forEach(key =>  {
      Object.defineProperty(vm, key, {
        enumerable: true,
        configurable: true,
        get() {
          console.log(`get: ${data[key]}`)
          return data[key]
        },
        set(newValue) {
          console.log(`set: ${newValue}`)
          if (newValue === data[newValue]) {
            return;
          }
          data[key] = newValue
          document.getElementById('app').textContent = data[key];
        }
      })
    })
  }
  proxyData(data)
  vm.name = 'ShaoChen Sun'
  console.log(vm.name)
</script>
</body>
</html>